package stc.skymobi.netty.transport;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import stc.skymobi.netty.transport.endpoint.Endpoint;
import stc.skymobi.util.MutablePropertyable;
import stc.skymobi.util.Propertyable;

public class TransportUtils {
    private static final AttributeKey<Endpoint> TRANSPORT_ENDPOINT = AttributeKey.valueOf("TRANSPORT_ENDPOINT");
    private static final String TRANSPORT_SENDER = "TRANSPORT_SENDER";

    public static void attachEndpointToSession(ChannelHandlerContext ctx, Endpoint endpoint) {
		ctx.attr(TRANSPORT_ENDPOINT).set(endpoint);
	}

	public static Endpoint getEndpointOfSession(ChannelHandlerContext ctx) {
		return	ctx.attr(TRANSPORT_ENDPOINT).get();
	}
	
	public static Object attachSender(Object mutablePropertyable, Sender sender) {
		if ( mutablePropertyable instanceof MutablePropertyable) {
			((MutablePropertyable)mutablePropertyable).setProperty(
					TRANSPORT_SENDER, sender);
		}
		
		return	mutablePropertyable;
	}
	
	public static Sender getSenderOf(Object propertyable) {
		if ( propertyable instanceof Propertyable) {
			return (Sender)((Propertyable)propertyable).getProperty(TRANSPORT_SENDER);
		}
		return null;
	}
}
